package leetcode.code2170;

import java.util.HashMap;
import java.util.Map;

//SolutionTest
public class Solution {

	public int minimumOperations(int[] nums) {
		Map<Integer, Integer> map1 = new HashMap<>();
		Map<Integer, Integer> map2 = new HashMap<>();
		int len = nums.length;
		for (int i = 0; i < len; i++) {
			int cur = nums[i];
			if ((i & 1) == 0) {
				map1.put(cur, map1.getOrDefault(cur, 0) + 1);
			} else {
				map2.put(cur, map2.getOrDefault(cur, 0) + 1);
			}
		}
		int[] cnt1 = this.top12(map1);
		int[] cnt2 = this.top12(map2);
		if (cnt1[0] == cnt2[0]) {
			int sum1 = cnt1[2] + cnt2[3];
			int sum2 = cnt2[2] + cnt1[3];
			return sum1 > sum2 ? len - sum1 : len - sum2;
		} else {
			return len - cnt1[2] - cnt2[2];
		}
	}

	private int[] top12(Map<Integer, Integer> map) {
		int[] ans = new int[4];
		for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
			int num = entry.getKey(), value = entry.getValue();
			if (value > ans[2]) {
				ans[1] = ans[0];
				ans[3] = ans[2];
				ans[0] = num;
				ans[2] = value;
			} else if (value > ans[3]) {
				ans[1] = num;
				ans[3] = value;
			}
		}
		return ans;
	}

}
